package com.sinoglobal.sinostore.activity;

import android.app.ProgressDialog;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest.HttpMethod;
import com.lidroid.xutils.util.LogUtils;
import com.sinoglobal.sinostore.R;
import com.sinoglobal.sinostore.ShareAbstractActivity;
import com.sinoglobal.sinostore.adapter.GoodsRecommendedAdapter;
import com.sinoglobal.sinostore.bean.ShopCityFirstVo;
import com.sinoglobal.sinostore.system.Constants;
import com.sinoglobal.sinostore.utils.ApiDebugUtil;
import com.sinoglobal.sinostore.widget.HorizontalListView;
/**
 * 
* @title: PayForSuccessActivity.java 
* @package com.chiliring.sinostore.activity 
* @description: TODO(用一句话描述该文件做什么) 
* @author LK
* @date 2015-1-16 下午5:31:00
 */
public class PayForSuccessActivity extends ShareAbstractActivity implements OnClickListener {
	
	/**相关推荐**/
	private HorizontalListView lvRecommended;
	
	/**查看订单**/
	private TextView btnLookOrder;
	
	/**分享按钮**/
	private TextView btnShare;
	/**提示信息**/
	private TextView tvMessage;
	/**购买成功**/
	private TextView tvShopMessage;
	
	private GoodsRecommendedAdapter recommendedAdapter;
	
	private String goodsName;
	
	private String goodsType;
	
	private String submitType;
	
	private String validDate;
	
	private String goodsImg;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.shop_activity_payfor_success);
		titleView.setText("支付成功");
		openChenJInShi();
		initView();
		
		initData();
	}
	
	private void initView(){
		lvRecommended=getViewById(R.id.lv_recommended);
		btnLookOrder=getViewById(R.id.btn_lookorder);
		btnShare=getViewById(R.id.btn_share);
		tvMessage=getViewById(R.id.tv_message);
		tvShopMessage = getViewById(R.id.tv_shop_message);
		
		
		
		btnLookOrder.setOnClickListener(this);
		btnShare.setOnClickListener(this);
		lvRecommended.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				Bundle bundle=new Bundle();
				bundle.putString("goodsId", recommendedAdapter.getItem(arg2).getId());
				toActivity(GoodsDetailActivity.class, bundle);
				
			}
		});
	}
	
	private void initData(){
		goodsName = getIntent().getStringExtra("goodsName");
		goodsType = getIntent().getStringExtra("goodsType");
		submitType = getIntent().getStringExtra("submitType");
		validDate = getIntent().getStringExtra("validDate");
		goodsImg = getIntent().getStringExtra("goodsImg");
		if(Constants.VIRTUALGOODS.equals(goodsType)){
			tvMessage.setText(getString(R.string.payfor_success_prompt_xuni));
		}else{
			if("1".equals(submitType)){//自取
				tvMessage.setText(getString(R.string.payfor_success_prompt_ziqu,validDate));
			}else{
				tvMessage.setText(getString(R.string.payfor_success_prompt));
			}
		}
		tvShopMessage.setText(getString(R.string.share_to_friend,getString(R.string.app_name)));
		HttpUtils httpUtils = new HttpUtils();
		RequestParams params = new RequestParams();
		params.addQueryStringParameter("por","109");
		params.addQueryStringParameter("proIden",Constants.STOREFLAG);
		params.addQueryStringParameter("type", "1");
		ApiDebugUtil.debug(params);
		final ProgressDialog progressDialog=new ProgressDialog(this);
		try {
			progressDialog.show();
		} catch (Exception e) {
		}
		httpUtils.send(HttpMethod.POST, Constants.STORE_URL,params, new RequestCallBack<String>() {
			@Override
			public void onFailure(HttpException arg0, String arg1) {
				try {
					if(progressDialog.isShowing()){
						progressDialog.dismiss();
					}
				} catch (Exception e) {
				}
			}

			@Override
			public void onSuccess(ResponseInfo<String> responseInfo) {
				try {
					if(progressDialog.isShowing()){
						progressDialog.dismiss();
					}
				} catch (Exception e) {
				}
				String result = responseInfo.result;
				if(!TextUtils.isEmpty(result)){
					LogUtils.e("result=="+result);
					ShopCityFirstVo cityFirstVo;
					try {
						cityFirstVo = JSON.parseObject(result, ShopCityFirstVo.class);
					} catch (Exception e) {
						e.printStackTrace();
						showShortToast("数据解析错误");
						return;
					}
					if(Constants.STORE_SUCCESS_CODE.equals(cityFirstVo.getCode())){
						if(null != cityFirstVo.getList() && cityFirstVo.getList().size() > 0){
							recommendedAdapter = new GoodsRecommendedAdapter(PayForSuccessActivity.this);
							recommendedAdapter.addData(cityFirstVo.getList());
							lvRecommended.setAdapter(recommendedAdapter);
						}
					}
				}
			}
		});
	}

	@Override
	public void onClick(View v) {
		int id = v.getId();
		if (id == R.id.btn_lookorder) {
			toActivity(OrderListActivity.class, null);
		} else if (id == R.id.btn_share) {
			String shareUrl = getString(R.string.shareUrl);
			String appName = getResources().getString(R.string.app_name);
			String text ="我在"+ appName+"购买了"+goodsName+",太划算了!"+shareUrl;
			setShare(text, getString(R.string.shareTitle), null, shareUrl, 0, Constants.PIC_HOST + goodsImg);
		} else {
		}
	}
}
